var express = require('express');
var router = express.Router();
var { cartModel, After_sales_managementModel, shipmentModel } = require('../../../model/model');

// ------------------订单--------------------
// 获取订单
router.get('/getOrders', async (req, res) => {
  let data = await cartModel.find().populate('userid').populate('goodsid').populate('groupid')
  res.send({
    code: 200,
    msg: '获取订单成功',
    data
  })
})

// 查看订单详情,
router.get('/getorderdetail', async (req, res) => {
  let _id = req.query.data._id
  console.log(_id);
  let data = await cartModel.find({ _id }).populate('userid').populate('goodsid').populate('groupid')
  res.send({
    code: 200,
    msg: '获取订单详情成功',
    data
  });
})

// 获取售后信息
router.get('/getAfter_sales_management', async (req, res) => {
  let user = req.query.data;
  let arr = []
  if (user != undefined) {
    if (user.from.order_id != '') {
      arr.push({ order_id: user.from.order_id })
    }
    if (user.from._id != '') {
      arr.push({ _id: user.from._id })
    }
    if (user.from.zitidian != '') {
      arr.push({ zitidian: user.from.zitidian })
    }
    if (user.from.zitidiantype != '请选择自提点') {
      arr.push({ zitidiantype: user.from.zitidiantype })
    }
    if (user.from.After_sales_state != '请选择售后类型') {
      arr.push({ After_sales_type: user.from.After_sales_state })
    }
    console.log(arr);
    if (arr.length > 0) {
      let data = await After_sales_managementModel.find({ $and: arr }).populate('order_id')
      console.log(data);
      res.send({
        code: 200,
        msg: '获取售后信息成功',
        data
      })
    } else {
      let data = await After_sales_managementModel.find().populate('order_id')
      res.send({
        code: 200,
        msg: '获取售后信息成功',
        data: data
      })
    }
  } else {
    let data = await After_sales_managementModel.find().populate('order_id')
    res.send({
      code: 200,
      msg: '获取售后信息成功',
      data: data
    })
  }
})

// 修改状态信息
router.post('/updateAfter_sales_management', async (req, res) => {
  let list_id = req.body.data.from.list_id
  let item = req.body.data.from.item
  console.log(list_id, item);
  let data = await After_sales_managementModel.updateMany({ _id: { $in: list_id } }, { $set: { After_sales_state: item } })
  res.send({
    code: 200,
    msg: '修改售后信息成功',
    data
  })
})
// 获取售后详细信息
router.get('/getAfter_sales_management_detail', async (req, res) => {
  let _id = req.query.data._id
  console.log(_id);
  let data = await After_sales_managementModel.findOne({ _id })

  res.send({
    code: 200,
    msg: '获取售后详细信息成功',
    data
  })
})

// 发货
// 获取售后详细信息
router.get('/getshipment',async(req,res)=>{
  // let type = req.query.data.type
  let data = await shipmentModel.find().populate('userid').populate('goodsid').populate('cart_id').populate('groupid')
  // console.log(data);
  res.send({
    code:200,
    msg:'获取发货信息成功',
    data
  })
})

module.exports = router;
